#ifndef MULTIKEY_H_
#define MULTIKEY_H_
#include "../record.h"
#include "../binarystream.h"
#include "../../common/define.h"

#include <cstdint>
#include <vector>
#include <fstream>
#include <sstream>

class MultiKey : public Key{
public:
	MultiKey() {
		this->comparisonPos = 0;
		this->keys = new u_int32_t*[DIMENSION_CLAVES];
		for (int i = 0; i < DIMENSION_CLAVES ; ++i)
			this->keys[i] = new u_int32_t;
	}
	void inicializar(){
		this->comparisonPos = 0;
		this->keys = new u_int32_t*[DIMENSION_CLAVES];
		for (int i = 0; i < DIMENSION_CLAVES ; ++i)
			this->keys[i] = new u_int32_t;
	}
	MultiKey(u_int32_t** keys) {
		this->comparisonPos = 0;
		this->keys = new u_int32_t*[DIMENSION_CLAVES];
		for (int i = 0; i < DIMENSION_CLAVES ; ++i)
			this->keys[i] = keys[i];
	}

	u_int32_t getKeysPos(u_int32_t pos) {
		return *this->keys[pos];
	}

	void read(BinaryStream& in) {
		this->inicializar();
		for (int i = 0; i < DIMENSION_CLAVES ; ++i)
			*this->keys[i] = in.readPrimitive<u_int32_t>();
	}
	void write(BinaryStream& out) {
		for (int i = 0; i < DIMENSION_CLAVES ; ++i)
			out.writePrimitive<u_int32_t>(*this->keys[i]);
	}

	int8_t compareTo(const Key* key) {
		std::cout << "MultiKey::compareTo..." << std::endl;
		MultiKey* that = (MultiKey*) key;

		for ( int i = 0 ; i < DIMENSION_CLAVES; ++i){
			if (*this->keys[i] < *that->keys[i]){
				return -1;
			}
			if (*this->keys[i] > *that->keys[i]){
				return 1;
			}
		}
		return 0;
	}

	std::string toString(){
		std::stringstream sout;
		for (int i = 0; i < DIMENSION_CLAVES ; ++i){
			sout << "Clave: " << *this->keys[i] << " ";
		}
		return sout.str();
	}

	void setComparisonPos(u_int32_t keyNumber){
		this->comparisonPos = keyNumber;
	}

private:
	  u_int32_t** keys;
	  u_int32_t  comparisonPos;
};

#endif /*MULTIKEY_H_*/
